import { ElMessage, ElMessageBox, ElNotification } from "element-plus";
import { debounce } from "lodash-es";

// 消息提示类型
type MessageType = "success" | "warning" | "info" | "error";

/**
 * 防抖消息提示
 * 用于解决快速点击按钮时，多次弹出消息提示的问题
 */
export const debounceMessage = debounce(
  (type: MessageType, content: string, duration = 3000) => {
    ElMessage({
      type,
      message: content,
      duration,
    });
  },
  500
);

/**
 * 成功消息
 */
export const successMessage = (content: string, duration = 3000) => {
  debounceMessage("success", content, duration);
};

/**
 * 错误消息
 */
export const errorMessage = (content: string, duration = 3000) => {
  debounceMessage("error", content, duration);
};

/**
 * 警告消息
 */
export const warningMessage = (content: string, duration = 3000) => {
  debounceMessage("warning", content, duration);
};

/**
 * 信息消息
 */
export const infoMessage = (content: string, duration = 3000) => {
  debounceMessage("info", content, duration);
};

/**
 * 确认对话框
 */
export const confirmDialog = (
  message: string,
  title = "确认操作",
  type: MessageType = "warning"
) => {
  return ElMessageBox.confirm(message, title, {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type,
  });
};

/**
 * 删除确认对话框
 */
export const deleteConfirm = (itemName: string) => {
  return confirmDialog(
    `确定要删除 "${itemName}" 吗？此操作不可恢复。`,
    "确认删除",
    "warning"
  );
};

/**
 * 通知提示
 */
export const showNotification = (
  title: string,
  message: string,
  type: MessageType = "info",
  duration = 4500
) => {
  ElNotification({
    title,
    message,
    type,
    duration,
  });
};

/**
 * 加载提示
 */
export const showLoading = (text = "加载中...") => {
  const loading = ElLoading.service({
    lock: true,
    text,
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
  return loading;
};

/**
 * 复制到剪贴板
 */
export const copyToClipboard = async (text: string) => {
  try {
    await navigator.clipboard.writeText(text);
    successMessage("已复制到剪贴板");
    return true;
  } catch (error) {
    console.error("复制失败:", error);
    errorMessage("复制失败");
    return false;
  }
};

/**
 * 下载文件
 */
export const downloadFile = (url: string, filename: string) => {
  const link = document.createElement("a");
  link.href = url;
  link.download = filename;
  link.target = "_blank";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

/**
 * 格式化文件大小
 */
export const formatFileSize = (bytes: number): string => {
  if (bytes === 0) return "0 B";

  const k = 1024;
  const sizes = ["B", "KB", "MB", "GB", "TB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
};

/**
 * 防抖函数
 */
export const debounce = <T extends (...args: any[]) => any>(
  func: T,
  wait: number
): ((...args: Parameters<T>) => void) => {
  let timeout: NodeJS.Timeout;

  return (...args: Parameters<T>) => {
    clearTimeout(timeout);
    timeout = setTimeout(() => func(...args), wait);
  };
};

/**
 * 节流函数
 */
export const throttle = <T extends (...args: any[]) => any>(
  func: T,
  limit: number
): ((...args: Parameters<T>) => void) => {
  let inThrottle: boolean;

  return (...args: Parameters<T>) => {
    if (!inThrottle) {
      func(...args);
      inThrottle = true;
      setTimeout(() => (inThrottle = false), limit);
    }
  };
};
